﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace DATN.mds.MailManagement
{
    public partial class EditMailForm : System.Web.UI.Page
    {
        private string cnnString;
        SqlConnection cnn;
        SqlCommand cmd;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Us_Id"] != null)
                {
                    //Do something here!
                }
                else
                {
                    Response.Redirect("../Account/Login.aspx");
                }
                Validate();
            }
        }

        //Lưu lại template
        protected void btnSaveMailTemp_Click(object sender, EventArgs e)
        {
            string TempName = temp_Name.Text;
            string TempTitle = temp_Title.Text;
            string TempContent = temp_Content.Text;

            if (TempName == null && TempName != "" && TempTitle != null && TempTitle != "")
            {
                int us_id = Convert.ToInt16(Session["Us_Id"].ToString());
                try
                {
                    cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                    cnn = new SqlConnection(cnnString);
                    cnn.Open();
                    cmd = cnn.CreateCommand();
                    cmd.CommandText = "INSERT INTO [dbo].[MailForm] ([Name], [Title], "
                        +"[ContentMail]) VALUES (@Name, @Title, @ContentMail)";

                    cmd.Parameters.Add("@Name", SqlDbType.NChar,50);
                    cmd.Parameters.Add("@Title", SqlDbType.NVarChar, 200);
                    cmd.Parameters.Add("@ContentMail", SqlDbType.NText);

                    cmd.Parameters["@Name"].Value = TempName;
                    cmd.Parameters["@Title"].Value = TempTitle;
                    cmd.Parameters["@ContentMail"].Value = TempContent;

                    cmd.ExecuteNonQuery();

                    Response.Write("Da Update thanh cong");
                    cnn.Close();
                }
                catch (Exception ex)
                {
                    Response.Write(ex);
                }
            
            }
        }

        //Nút Back Về trang Chủ
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("../mds.MailManagement/SendMailList.aspx");
        }

    
    }
}